// pages/store/store.js
const request = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dianpu_data : {},
    xiyiji_list : [],
    storeId:'',
    isRepairModalVisible: false,
    repairContent: '',
    washingMachineId: null,
  },

  finddianpu(){
    const param = {
      id:this.storeId
    }
    request.post("/index/findianpudata",param)
    .then(res=>{
      console.log(res);
      this.setData({
        dianpu_data: res.data,
      });
      // this.dianpu_data = res.data;
    }).catch(res=>{
    })
  },

  findxiyiji(){
    const param = {
      merchantId:this.storeId
    }
    request.post("/index/finxiyi",param)
    .then(res=>{
      console.log(res);
      this.setData({
        xiyiji_list: res.data,
      });
      console.log(this.xiyiji_list)
    }).catch(res=>{
    })
  },

  useWashingMachine:function(event){
    const xiyiid = event.currentTarget.dataset.id;
    const param = {
      washingMachineId: xiyiid,
      merchantId:this.storeId
    }
    console.log('选中洗衣机:', param);
    request.post("/index/addxiyi",param)
    .then(res =>{
      console.log(res);
      if (res.code === 0) {
        wx.showToast({ title: res.msg, icon: 'none' });
        this.findxiyiji();
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.storeId = options.storeId;
    console.log(this.storeId)
    this.finddianpu();
    this.findxiyiji();
  },
  
// 显示报修弹出框
showRepairModal(e) {
  const id = e.currentTarget.dataset.id;
  console.log(id);
  this.setData({
    isRepairModalVisible: true,
    washingMachineId: id,
  });
},

// 关闭报修弹出框
closeRepairModal() {
  this.setData({
    isRepairModalVisible: false,
    repairContent: '',  // 清空输入框
  });
},

// 监听输入框内容变化
onRepairContentChange(e) {
  this.setData({
    repairContent: e.detail.value,
  });
},

// 提交报修
submitRepair() {
  if (!this.data.repairContent) {
    wx.showToast({
      title: '请输入报修问题',
      icon: 'none',
    });
    return;
  }
  const param = {
    washingMachineId: this.data.washingMachineId,
    repairDescription: this.data.repairContent,
  };
  request.post('/index/addrepair', param)
  .then((res) => {
    wx.showToast({
      title: '报修提交成功',
      icon: 'success',
    });
    this.closeRepairModal();
  });
  
},
})